package com.lastcrdg.testmvp;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/8/17 16:54
 * description：解耦
 */

public class BasePresenter<V> {

    protected Reference<V> mReference;
    protected V view;

    public void attach(V view){
        mReference = new WeakReference<V>(view);
        view = mReference.get();
    }

    public V getView(){
        if (mReference == null){
            return  null;
        }else {
            return mReference.get();
        }
    }

    public boolean isAttach(){
        return mReference != null && mReference.get() != null;
    }

    public void detach(){
        if (mReference != null){
            mReference.clear();
            mReference = null;
        }
    }
}
